﻿using Microsoft.AspNet.Identity;
using System;
using System.Text;
using System.Windows.Forms;

namespace ABPTool
{
    public partial class FormABPTool : Form
    {
        public FormABPTool()
        {
            InitializeComponent();
        }

        private void btnBulidPermissionCode_Click(object sender, EventArgs e)
        {
            string permissionCode = txtPermissionCode.Text.Trim();
            string permissionName = txtPermissionName.Text.Trim();
            if (string.IsNullOrWhiteSpace(permissionCode) || string.IsNullOrWhiteSpace(permissionName))
            {
                MessageBox.Show("权限编码、权限名称都不能为空！！");
                return;
            }

            txtVariateCode.Text = GetVariateCode(permissionCode, permissionName).ToString();
            txtProviderCode.Text = GetProviderCode(permissionCode, permissionName).ToString();
            txtLocalizationCode.Text = GetLocalizationCode(permissionCode, permissionName, cbHasCURD.Checked).ToString();
        }

        /// <summary>
        /// 变量代码
        /// </summary>
        /// <param name="permissionCode"></param>
        /// <param name="permissionName">权限名称</param>
        /// <returns></returns>
        private StringBuilder GetVariateCode(string permissionCode, string permissionName)
        {
            StringBuilder strb = new StringBuilder();
            strb.AppendLine("//" + permissionName);
            strb.AppendLine(string.Format("public const string Pages_{0} = \"Pages.{0}\";", permissionCode));
            strb.AppendLine(string.Format("public const string Pages_{0}_Create = \"Pages.{0}.Create\";", permissionCode));
            strb.AppendLine(string.Format("public const string Pages_{0}_Delete = \"Pages.{0}.Delete\";", permissionCode));
            strb.AppendLine(string.Format("public const string Pages_{0}_Edit = \"Pages.{0}.Edit\";", permissionCode));
            strb.AppendLine(string.Format("public const string Pages_{0}_Select = \"Pages.{0}.Select\";", permissionCode));
            return strb;
        }

        /// <summary>
        /// Provider类代码
        /// </summary>
        /// <param name="permissionCode">权限编码</param>
        /// <param name="permissionName">权限名称</param>
        /// <returns></returns>
        private StringBuilder GetProviderCode(string permissionCode, string permissionName)
        {
            StringBuilder strb = new StringBuilder();
            strb.AppendLine(string.Format("//{0}权限", permissionName));
            strb.AppendLine(string.Format("Permission per{0} = context.CreatePermission(PermissionNames.Pages_{0}, L(\"{0}\"));", permissionCode));
            strb.AppendLine(string.Format("per{0}.CreateChildPermission(PermissionNames.Pages_{0}_Create, L(\"Create\"));", permissionCode));
            strb.AppendLine(string.Format("per{0}.CreateChildPermission(PermissionNames.Pages_{0}_Delete, L(\"Delete\"));", permissionCode));
            strb.AppendLine(string.Format("per{0}.CreateChildPermission(PermissionNames.Pages_{0}_Edit, L(\"Edit\"));", permissionCode));
            strb.AppendLine(string.Format("per{0}.CreateChildPermission(PermissionNames.Pages_{0}_Select, L(\"Select\"));", permissionCode));

            return strb;
        }

        /// <summary>
        /// 本地语言打码
        /// </summary>
        /// <param name="permissionCode">权限编码</param>
        /// <param name="permissionName">权限名称</param>
        /// <param name="hasCURD">是否需要增删改查翻译</param>
        /// <returns></returns>
        private StringBuilder GetLocalizationCode(string permissionCode, string permissionName, bool hasCURD = false)
        {
            StringBuilder strb = new StringBuilder();
            strb.AppendLine(string.Format("<!--{0}-->", permissionName));
            strb.AppendLine(string.Format("<text name=\"{0}\" value=\"{1}\"/>", permissionCode, permissionName));
            if (hasCURD)
            {
                strb.AppendLine(string.Format("<text name=\"Create\" value=\"新建\"/>", permissionCode));
                strb.AppendLine(string.Format("<text name=\"Delete\" value=\"删除\"/>", permissionCode));
                strb.AppendLine(string.Format("<text name=\"Edit\" value=\"编辑\"/>", permissionCode));
                strb.AppendLine(string.Format("<text name=\"Select\" value=\"查询\"/>", permissionCode));
            }
            return strb;
        }

        private void btnBulidPwd_Click(object sender, EventArgs e)
        {
            var pwd = txtPwd.Text.Trim();
            string pwd2 = new PasswordHasher().HashPassword(pwd);
            txtPwd2.Text = pwd2;
        }
    }
}
